﻿unit FileInfo;

interface

uses
 SysUtils,Classes,Windows, Messages, Variants, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ToolWin, R4FuncKeys, Grids, R4Grid, Buttons;

type
  TFileInfo = record
    FileName : string;
    FilePath : string;
    FileType : string;
    FileSize : Int64;
    FileDate : string;
  end;
  PFileInfo = ^TFileInfo;

procedure InitFileInfo(var FileInfo : TFileInfo;FileName,FilePath,FileType : string; FileSize : Int64; FileDate : string);

function SortByName(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByPath(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByType(FileInfo1,FileInfo2 : Pointer):Integer;

function SortBySize(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByDate(FileInfo1,FileInfo2 : Pointer):Integer;

procedure Sorts(var List : TList;Num:Integer;IsSorted:Integer);

implementation

var
  Sorted : Integer;

procedure InitFileInfo(var FileInfo : TFileInfo; FileName,FilePath,FileType : string; FileSize : Int64; FileDate : string);
begin
  FileInfo.FileName := FileName;
  FileInfo.FilePath := FilePath;
  FileInfo.FileType := FileType;
  FileInfo.FileSize := FileSize;
  FileInfo.FileDate := FileDate;
end;

function SortByName(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;


function SortByPath(FileInfo1,FileInfo2 : Pointer):Integer;
begin

  Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

end;

function SortByType(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileType,PFileInfo(FileInfo1)^.FileType);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;

function SortBySize(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := PFileInfo(FileInfo2)^.FileSize - PFileInfo(FileInfo1)^.FileSize;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;

function SortByDate(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileDate,PFileInfo(FileInfo1)^.FileDate);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;
end;

procedure Sorts(var List : TList;Num:Integer;IsSorted:Integer);
var
  Temp : PFileInfo;
  I,J : Integer;
begin

  case Num of
    1 : List.Sort(SortByName);
    2 : List.Sort(SortByPath);
    3 : List.Sort(SortByType);
    4 : List.Sort(SortBySize);
    5 : List.Sort(SortByDate);
  end;

  //如果已经是降序排序，就反转List
  if(IsSorted = 1) then
  begin
    J := (List.Count -1) div 2;
    //showmessage(IntToStr(J));
    for I := 0 to J do
    begin
      Temp := List[I];
      List[I] := List[List.Count-1-I];
      List[List.Count-1-I] := Temp;
    end;
  end;
end;

end.
